import warnings
warnings.filterwarnings("ignore")
import numpy as np
from flask import Flask, request, jsonify
from keras.models import load_model
import pickle
import numpy as np
from gevent.pywsgi import WSGIServer


#加载scaler
with open('humd_s.pkl', 'rb') as fr:
    humd_s = pickle.load(fr)
    
#加载模型
humd_model = load_model('humd_model.h5')
app = Flask(__name__)

@app.route('/humd', methods={'get'})
def humd_pred():
    data = request.args.get("humd_data",type=str,default=None)
    print('data: ', data)
    humd_datas = eval(data)
    print('begin******')
    print('humd_datas: ', humd_datas)
    humd_datas = np.array(humd_datas)
    #清洗数据
    data_process = humd_s.transform(humd_datas)
    data_process = data_process.reshape(humd_datas.shape[0], 1, humd_datas.shape[1])
    print(data_process.shape)
    humd_pred = humd_model.predict(data_process)
    print('humd_pred:',humd_pred)
    humd_result = humd_s.inverse_transform(humd_pred).reshape(humd_pred.shape[1], 1)
    humd_result = np.squeeze(humd_result)
    print(humd_result)
    return jsonify({'humd_result': str(humd_result)})
    
if __name__ == '__main__':
    WSGIServer(('0.0.0.0', 5000), app).serve_forever()